﻿#include "Titlebar.h"

#include <QHBoxLayout>
#include <QDebug>

#include <QHutu.h>

namespace Softwarer
{
    ITitlebar::ITitlebar(float height, QWidget* parent)
        : QWidget(parent)
    {
        
        setFixedHeight(30);
        setWindowFlags(Qt::FramelessWindowHint);
        this->setObjectName("titlebar");
        this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        this->setAttribute(Qt::WA_StyledBackground);
        QString qss = QHutu::readResourceFile("QSS/Titlebar.qss");
        this->setStyleSheet(qss);
    }

    ITitlebar::~ITitlebar()
    {
    }

    void ITitlebar::mousePressEvent(QMouseEvent* event)
    {
        // 鼠标左键按下事件
        if (event->button() == Qt::LeftButton)
        {
            // 记录鼠标左键状态
            m_leftButtonPressed = true;
            //记录鼠标在屏幕中的位置
            m_start = event->globalPos();
        }
    }

    void ITitlebar::mouseMoveEvent(QMouseEvent* event)
    {
        // 持续按住才做对应事件
        if (m_leftButtonPressed)
        {
            QWidget* mwd = getControlledWidget();
            mwd->move(mwd->geometry().topLeft() + event->globalPos() - m_start);
            m_start = event->globalPos();
        }
    }

    void ITitlebar::mouseReleaseEvent(QMouseEvent* event)
    {
        // 鼠标左键释放
        if (event->button() == Qt::LeftButton)
        {
            // 记录鼠标状态
            m_leftButtonPressed = false;
        }
    }

    QWidget* ITitlebar::getControlledWidget()
    {
        QWidget* pwd = parentWidget();
        if (pwd == nullptr)
        {
            return this;
        }
        return pwd;
    }

    QLabel* ITitlebar::createIconLabel(int w, int h)
    {
        m_iconLabel = new QLabel(this);
        m_iconLabel->setFixedSize(w, h);
        m_iconLabel->setScaledContents(true);

        QPixmap pixmap(QHutu::resourceDirPath("Images/Icons/Logo.png"));

        // 确保支持alpha通道
        pixmap = pixmap.scaled(w * 0.9, h * 0.9, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        m_iconLabel->setPixmap(pixmap);

        // 设置背景透明（如果需要）
        m_iconLabel->setAttribute(Qt::WA_TranslucentBackground);
        m_iconLabel->setStyleSheet("background: transparent;");
       
        return m_iconLabel;
    }

    QLabel* ITitlebar::createTitleLabel(const QString& title)
    {
        m_titleLabel = new QLabel(this);
        m_titleLabel->setText(title);
        m_titleLabel->setObjectName("titleLabel");
        m_titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        return m_titleLabel;
    }

    void ITitlebar::OnCloseSlot() 
    {
        getControlledWidget()->close();
    }
    void ITitlebar::OnMinimizeSlot()
    {
        getControlledWidget()->showMinimized();
    }

    void ITitlebar::OnMaximizeSlot()
    {
        getControlledWidget()->showMaximized();
    }
    QPushButton* ITitlebar::createMinimizeButton(int w, int h)
    {
        m_minimizeButton = new QPushButton(this);
        m_minimizeButton->setFixedSize(w, h);

        m_minimizeButton->setObjectName("MinimizeButton");
        m_minimizeButton->setIcon(QIcon(QHutu::resourceDirPath("Images/Icons/Minimize.png")));
        m_minimizeButton->setIconSize(QSize(w * 6 / 10, h * 6 / 10));
        connect(m_minimizeButton, SIGNAL(clicked()), this, SLOT(OnMinimizeSlot()));

        return m_minimizeButton;
    }

    QPushButton* ITitlebar::createMaximizeButton(int w, int h) 
    {
        m_maximizeButton = new QPushButton(this);
        m_maximizeButton->setFixedSize(w, h);

        m_maximizeButton->setObjectName("MaximizeButton");
        m_maximizeButton->setIcon(QIcon(QHutu::resourceDirPath("Images/Icons/Maximize.png")));
        m_maximizeButton->setIconSize(QSize(w * 6 / 10, h * 6 / 10));
        connect(m_maximizeButton, SIGNAL(clicked()), this, SLOT(OnMaximizeSlot()));

        return m_maximizeButton;
    }

    QPushButton* ITitlebar::createCloseButton(int w, int h)
    {
        m_closeButton = new QPushButton(this);
        m_closeButton->setFixedSize(w, h);
        
        m_closeButton->setObjectName("CloseButton");
        m_closeButton->setIcon(QIcon(QHutu::resourceDirPath("Images/Icons/Close.png")));
        m_closeButton->setIconSize(QSize(w * 6 / 10, h * 6 / 10));
        connect(m_closeButton, SIGNAL(clicked()), this, SLOT(OnCloseSlot()));

        return m_closeButton;
    }

    const float HEIGHT = 20.0f;

    ClsTitlebar::ClsTitlebar(QWidget* parent)
        : ITitlebar(HEIGHT, parent)
    {       
        QHBoxLayout* layout = new QHBoxLayout(this);
        layout->addWidget(createIconLabel(HEIGHT, HEIGHT));
        layout->addStretch(1);
        layout->addWidget(createTitleLabel(QString::fromLocal8Bit("汗血马引擎")));
        layout->addStretch(1);
        layout->addWidget(createCloseButton(HEIGHT, HEIGHT));
        layout->setContentsMargins(2, 2, 2, 2);
    }

    WinTitlebar::WinTitlebar(QWidget* parent)
        : ITitlebar(HEIGHT + 10.0f, parent)
    {
        QHBoxLayout* layout = new QHBoxLayout(this);
        layout->addWidget(createIconLabel(HEIGHT, HEIGHT));
        //layout->addWidget(createTitleLabel(QString::fromLocal8Bit("汗血马引擎")));
        QMenuBar* menubar = new QMenuBar(this);

        QString qss = QHutu::readResourceFile("QSS/QMenuBar.qss");
        menubar->setStyleSheet(qss);
        layout->addWidget(menubar);
        layout->addStretch(1);
        layout->addWidget(createMinimizeButton(HEIGHT, HEIGHT));
        layout->addWidget(createMaximizeButton(HEIGHT, HEIGHT));
        layout->addWidget(createCloseButton(HEIGHT, HEIGHT));
        layout->setContentsMargins(2, 2, 2, 2);

        QMenu* fileMenu = menubar->addMenu("文件(F)");
        QMenu* editMenu = menubar->addMenu("编辑(E)");

        fileMenu->addAction("新建");
        fileMenu->addAction("打开");
        //fileMenu->addAction(QString::fromLocal8Bit("保存"));
        //fileMenu->addAction("退出");

    
    }
}